package com.liner.homework;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework03 {
    static Scanner scanner = new Scanner(System.in);
    static Random rand = new Random();

    public static void main(String[] args) {
        /*
        1. 猜数字
        2. 生成N位验证码
        3. 找到2到100之内的所有素数
        4. 飞机票打折
        5. 评委打分:去掉最高分和最低分之后的平均分
*/


        // 猜数字
        int count = 10;
        System.out.println("系统生成1~100之间的数字，请你猜一猜（你有10次机会）");
        int random = rand.nextInt(100) + 1;
        guess(count, random);


        // 生成N位验证码
        System.out.print("请输入想要生成几位数验证码:");
        int len = scanner.nextInt();
        String s = generateCode(len);
        System.out.println("验证码为：" + s);


        //找到2到100之内的所有素数
        System.out.println("找到2到100之内的所有素数:");
        prime();

        System.out.println();
        //飞机票打折
        calAirPrice();


        //评委打分:去掉最高分和最低分之后的平均分
        System.out.print("请输入要评分评委的个数:");
        int people = scanner.nextInt();
        double[] scores = inputData(people);
        System.out.println("最后得分的平均分为:"  + getAvg(scores));


    }

    private static void calAirPrice() {
        System.out.print("请输入机票原价:");
        if (scanner.hasNextInt()) {
            int price = scanner.nextInt();
            System.out.print("请输入当前月份:");
            int month = scanner.nextInt();
            System.out.print("请输入预选舱位:(1.头等舱、2.商务舱、3.经济舱)");
            int type = scanner.nextInt();

            double finalPrice = calFinalPrice(price, month, type);
            if (finalPrice != -1) {
                System.out.println("订票成功,花费" + finalPrice + "元");
            }
        } else {
            System.out.println("请输入正确字符");
        }
    }

    /**
     * 求平均分
     * @param scores
     */
    private static double getAvg(double[] scores) {
        double sum = getSum(scores);
        return sum /(scores.length - 2);
    }

    /**
     * 求总分
     * @param scores
     * @return
     */
    private static double getSum(double[] scores) {
        double sum = 0;
        Arrays.sort(scores);
        for (int i = 1; i < scores.length -1; i++) {
            sum += scores[i];
        }
        return sum;
    }


    /**
     * 从控制台输入评委评分
     * @param people
     * @return
     */
    private static double[] inputData(int people) {
        double[] scores = new double[people];
        for (int i = 0; i < scores.length; i++) {
            System.out.print("请输入第" + (i + 1) + "位评委的评分:");
            scores[i] = scanner.nextDouble();
        }
        return scores;
    }

    /**
     * 打折机票
     *
     * @param price
     * @param month
     * @param type
     * @return
     */
    private static double calFinalPrice(int price, int month, int type) {

        double finalPrice;
        if (price < 0) {
            System.out.println("请输入正确价格");
            return -1;
        }
        if (month < 1 || month > 12) {
            System.out.println("请输入正确月份");
            return -1;
        }
        if (type < 1 || type > 3) {
            System.out.println("请输入正确舱位");
            return -1;
        }

        if (month >= 5 && month <= 10) {
            //旺季
            switch (type) {
                case 1:
                    finalPrice = price * 0.9;
                    break;
                case 2:
                    finalPrice = price * 0.85;
                    break;
                default:
                    finalPrice = price * 0.8;
            }

        } else {
            //淡季
            switch (type) {
                case 1:
                    finalPrice = price * 0.7;
                    break;
                case 2:
                    finalPrice = price * 0.65;
                    break;
                default:
                    finalPrice = price * 0.6;
            }
        }
        return finalPrice;
    }


    /**
     * 素数
     */
    private static void prime() {
        for (int i = 2; i <= 100; i++) {
            boolean flag = true;
            for (int j = 2; j < i / 2; j++) {

                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "\t");
            }
        }
    }

    /**
     * 生成验证码
     *
     * @param len
     * @return
     */
    private static String generateCode(int len) {
        String code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrlstuvwxyz";
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < len; i++) {
            stringBuilder.append(code.charAt(rand.nextInt(code.length())));
        }
        return stringBuilder.toString();
    }


    /**
     * 猜数字
     *
     * @param count
     * @param random
     */
    private static void guess(int count, int random) {
        while (true) {
            System.out.print("请输入你所猜测的数字:");
            int number = scanner.nextInt();
            if (number == random) {
                count--;
                System.out.println("恭喜你用了" + (10 - count) + "次机会猜对了");
                break;
            } else if (number > random) {
                System.out.println("很遗憾,猜大了");
                count--;
                System.out.println("你还剩" + count + "次机会");
            } else {
                System.out.println("很遗憾,猜小了");
                count--;
                System.out.println("你还剩" + count + "次机会");
            }

            if (count == 0) {
                System.out.println("机会用尽,下次再来！");
            }

        }
    }
}
